<?php


namespace app\controller;


use app\admin\model\AdminLog;
use app\BaseController;
use app\library\Auth;
use think\Config;
use think\Hook;
use think\Validate;

class Login extends BaseController {

    public function login()
    {
        $auth=new Auth();
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        $code = $this->request->post('code');
        if (!$account || !$password||!$code) {
            $this->res('-1','用户名密码错误');
        }
        $ret = $auth->login($account, $password,$code);
        if ($ret) {
            $data = ['userinfo' => $auth->getUserinfo()];
            $this->success(lang('Logged in successful'), $data);
        } else {
            $this->res('-2',$auth->getError());
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        if ($this->request->isPost()) {
            $this->auth->logout();
            //Hook::listen("admin_logout_after", $this->request);
            $this->success(__('Logout successful'), 'index/login');
        }
        $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
        $html .= "<script>document.forms['logout_submit'].submit();</script>";

        return $html;
    }

}